import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svprogresshud/flutter_svprogresshud.dart';
import 'package:goumao_app/Controller/gm_index_page.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';
import 'package:goumao_app/Customs/route_listen_widget.dart';
import 'package:provider/provider.dart';
import 'Controller/gm_home_page.dart';
import 'Controller/gm_login_center.dart';
import 'Controller/gm_mine_page.dart';
import 'Manager/gm_user_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  GMUserManager().loadUser();
  SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark);
  SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.clear);
  SVProgressHUD.setCornerRadius(6);
  SVProgressHUD.setMinimumDismissTimeInterval(1.5);
  runApp(
    ChangeNotifierProvider(
      create: (context) => GMUserManager(),
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    GMAppUtil.init(context);
    ScreenUtil.init(context);
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => GMUserManager()),
      ],
      child: MaterialApp(
      title: '宠物社交',
      debugShowCheckedModeBanner: false,
      debugShowMaterialGrid: false,
      theme: ThemeData(
        primarySwatch: GMAppUtil.primarySwatchColor,
      ),
      home: Consumer<GMUserManager>(
          builder: (context, userProvider, child) {
            // 根据用户登录状态显示不同的页面
            return userProvider.isLoggedIn ? const MainScreen() : const LoginPage();
          },
        ),
      navigatorObservers: [GMRouteListenWidget.routeObserver],
    ),
    );
  }
}

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  int _selectedIndex = 0;

  final List<Widget> _screens = [
    const GMIndexScreen(),
    HomeScreen(),
    const ProfileScreen(),
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _screens[_selectedIndex],
      bottomNavigationBar: ConvexAppBar(
        backgroundColor: const Color(0xffffffff),
        style: TabStyle.fixedCircle,
        activeColor: const Color(0xffFF6A6E),
        color: const Color(0xffB8BBBE),
        items: [
          TabItem(
              icon: Image.asset('assets/images/gm_tab_sy_n@2x.png'),
              activeIcon: Image.asset('assets/images/gm_tab_sy_s@2x.png'),
              title: "首页"),
          TabItem(
              icon: Image.asset(
                'assets/images/gm_centet_add@2x.png',
              ),
              activeIcon: Image.asset('assets/images/gm_center_s@2x.png')),
          TabItem(
              icon: Image.asset('assets/images/gm_tab_we_n@2x.png'),
              activeIcon: Image.asset('assets/images/gm_tab_wd_s@2x.png'),
              title: "我的"),
        ],
        initialActiveIndex: _selectedIndex,
        onTap: (int i) => {
          setState(() {
            _selectedIndex = i;
          })
        },
      ),
    );
  }
}
